'use strict';

/* Controllers */
app.controller('personalLandlordCtrl', function($scope, ngDialog, uiLoad, rest, user, dialog, $state, uploadConf, tipService) {

/* 个人个人个人个人个人个人个人个人个人个人二房东
对应接口编号： 4004增； 4022改； 4021查；
*/

  $scope.data = {
    landlordId: user.get('landlordId')
  };
  $scope.isShow = false;

  $scope.verifyFailMessage = "";
  $scope.verifying = false;
  $scope.verifyFail = false;
  $scope.verifyFail1= false;
  $scope.agreement = true;

  $scope.requestMethodId = "4004";
  $scope.checkId = undefined;

  $scope.column2 = 0;      //分栏
  $scope.maxImgNum = 100;    //最大图片数量
  $scope.imgNew= [
      {
          text:'二房东身份证正反面',
          name: 'landlordPicChange',
          value: [],
          danger:1
      },
      {
          text:'二房东业务负责人身份证正反面',
          name: 'landlordClientPicChange',
          value: [],          
      },
      {
          text:'委托业务负责人授权书',
          name: 'clientPicChange',
          value: [],
      },
      {
          text:'二房东相关收租流水',
          name: 'rentalPropertyCertificateChange',
          value: [],
          pdf:1
      }
      // {
      //     text:'二房东相关收租流水',
      //     name: 'rentalPropertyCertificateChange',
      //     value: [],
      // }
    ];   
       
    uploadConf.column2 = $scope.column2;
    uploadConf.maxImgNum =  $scope.maxImgNum;
    uploadConf.imgNew = $scope.imgNew;


  rest.post({
    methodId: '4012',
    data: {
      landlordId: user.get('landlordId')
    },
    success: function(data) {
      if (data.code == "0") {
        var data = data.res.data;
        // status  -1=未申请 0=申请中 1=审核通过 2=审核不通过
        // identityType [0=未申请 1=业主 2=二房东（个人） 3=二房东（公司）]
        if(data.identityType == 0 || data.identityType == 2){
          if (data.status == 0) {
            $scope.steps.step2 = true;
            $scope.verifying = true;
          } else if (data.status == 1) {
            $scope.steps.step3 = true;
          } else if(data.status == -1){
            init();
            $scope.steps.step1 = true;
          } else if(data.status == 2){
            $scope.verifyFail1 = true;
            $scope.steps.step2 = true;
            $scope.requestMethodId = "4022";
            $scope.verifyFailMessage = data.remark;
            $scope.checkId = data.checkId;
            getPersonalInfo();
          }else if(data.status == 3){
            $scope.verifyFail = true;
            $scope.steps.step2 = true;
            $scope.requestMethodId = "4022";
            $scope.verifyFailMessage = data.remark;
            $scope.checkId = data.checkId;
            getPersonalInfo();
          }
        }else{
          init();
          $scope.steps.step1 = true;
        }
        $scope.isShow = true;
        $scope.$digest();
      } else {
        dialog.tips(data.res.msg);
      }
    },
    error: function() {}
  });

  $scope.update = function(){
    $scope.steps.step1 = true;
  }

  /**
    上传业主房东身份证照片
  */
  $scope.$on('landlordPicChange', function(d, data) {
    $scope.data.idCard = data.list[0];
    $scope.data.idCardBack = data.list[1];
  });

  /**
    上传业主（房东）委托人身分证照片
  */
  $scope.$on('landlordClientPicChange', function(d, data) {
    $scope.data.clientCard = data.list[0];
    $scope.data.clientCardBack = data.list[1];
  });

  /**
   *  上传委托人代理人授权书
   */
 $scope.$on('clientPicChange', function(d, data) {
   $scope.data.entrust = data.list;
 });


  // 二房东相关收租流水
  $scope.$on('rentalPropertyCertificateChange', function(d, data){
    $scope.data.rental = data.list;
  });
  $scope.showTxt = function(contractId){
        ngDialog.openConfirm({
          template: 'js/module/fund/creditReport.html',
          resolve: {},
          data:{},
          scope: $scope,
          width: 800,
          height: 700
        }).then(function(data) {

        }, function(reason) {

        });
  }
  // 提交个人业主数据
  $scope.personalInfo = function() {
    $scope.verifying = false;
    $scope.verifyFail = false;
    $scope.verifyFail1 = false;
    console.log($scope.data);
    console.log(uploadConf.imgNew);
    $scope.data.idCard = {};
    $scope.data.idCardBack = {};
    $scope.data.clientCard = {};
    $scope.data.clientCardBack = {};
    $scope.data.entrust = [];
    $scope.data.rental = [];

    $scope.data.idCard = uploadConf.imgNew[0].value[0];
    $scope.data.idCardBack = uploadConf.imgNew[0].value[1];
    $scope.data.clientCard = uploadConf.imgNew[1].value[0];
    $scope.data.clientCardBack = uploadConf.imgNew[1].value[1];
    for (var i in uploadConf.imgNew[2].value) {
        $scope.data.entrust.push(uploadConf.imgNew[2].value[i]);
    }
    for (var i in uploadConf.imgNew[3].value) {
        $scope.data.rental.push(uploadConf.imgNew[3].value[i]);
    }

    for (var i in $scope.imgNew) {
       if ($scope.imgNew[0].value.length < 2) {
        tipService.show("body", {
            message: "需要上传业主房东身份证正反两张照片",
            notice: "danger"
        }, 3);
        return;
      }
    }
    if ($scope.data.phone.length != 11 || $scope.data.phone.charAt(0) != 1) {
        tipService.show("body", {
            message: "二房东手机号格式输入错误",
            notice: "danger"
        }, 3);
        return;
    }
    console.log($scope.imgNew);
    if ($scope.data.clientPhone) {
        if ($scope.data.clientPhone.length != 11 || $scope.data.clientPhone.charAt(0) != 1) {
          tipService.show("body", {
              message: "二房东业务负责人手机号格式输入错误",
              notice: "danger"
          }, 3);
          return;
        }
    }
    $scope.data.landlordName = $scope.data.landlordName.replace(/[ 　\[\]\\\/\"\'\-!@#$%^&*(){}<>,.?:;~`_+=]/g, "");
    
    rest.post({
      methodId: $scope.requestMethodId,
      data: $scope.data,
      success: function(data) {
        if (data.code == "0") {
          $scope.steps.step2 = true;
          $scope.verifying = true;
          $scope.$digest();
        } else {
          dialog.tips(data.res.msg);
        }
      },
      error: function() {}
    });
  }

  $scope.goHouseEdit = function () {
    // http://120.26.246.115:8020/index.html?landlordId=10039
    if (user.get('landlordId')) {
      $state.go('app.houseOwner.list');
      // location.href = "http://120.26.246.115:8020/index.html?landlordId=" + user.get('landlordId');
    }
    // console.log('goHouseEdit: %s', user.get('landlordId'));
  }

  $scope.goBack = function(){
    $state.go('nav.widget')
  }

  $scope.showlandlordPicTips = function(){
    dialog.tips("身份证拍照必须正反面清晰，无光线遮挡，无缺角与遮挡。（纸质与影像件）")
  }
  $scope.showlandlordClientPicTips = function(){
    dialog.tips("身份证拍照必须正反面清晰，无光线遮挡，无缺角与遮挡。（纸质与影像件）")
  }
  $scope.showclientPicTips = function(){
    dialog.tips("")
  }
  $scope.showrentalPropertyCertificateTips = function(){
    dialog.tips("提供的流水必须是房东的业务资金来往流水，流水金额能明确体现出企业的房租支付，水电煤物业费支付，房客租金收入等情况。（纸质与影像件）银行EXCEL表格无效。企业可以提供对外财务报表。")
  }

  function init(data){
    /**
     * 个人业主
     * 4005
     */

    $scope.data = {
      landlordId: user.get('landlordId'),
      idCardNo: !!data ? data.idCardNo : "",
      phone: !!data ? data.phone : "",
      clientPhone: !!data ? data.clientPhone : "",
      // idCard: !!data ? data.idCard : "",
      // idCardBack: !!data ? data.idCardBack : "",
      // clientCard: !!data ? data.clientCard : "",
      // clientCardBack: !!data ? data.clientCardBack : "",
      // entrust: !!data ? data.entrust : "",
      // rental: !!data ? data.rental : "",
      checkId: $scope.checkId,
      bankName: !!data ? data.bankName : "",
      bankAccount: !!data ? data.bankAccount : "",
      landlordName:!!data ? data.landlordName : "",
    };
    /**
      上传业主房东身份证照片
    */
    // $scope.landlordPic = !!data ? [data.idCard,data.idCardBack] : [];
    /**
      上传业主（房东）委托人身分证照片
    */
    // $scope.landlordClientPic = !!data ? [data.clientCard,data.clientCardBack] : [];
    /**
     *  上传委托人代理人授权书
     */

   // $scope.clientPic = !!data && data.entrust ? data.entrust : [];
   /**
   * 二房东相关收租流水
   */
   // $scope.rentalPropertyCertificate = !!data && data.rental ? data.rental : [];
   if (data) {
      // 上传二房东（法人）身份证照片
      $scope.imgNew[0].value = [];
      if (data.idCard)
          $scope.imgNew[0].value.push(data.idCard);
      if (data.idCardBack)
          $scope.imgNew[0].value.push(data.idCardBack);
      // // 上传二房东业务负责人身份证
      $scope.imgNew[1].value = [];
      if (data.clientCard)
          $scope.imgNew[1].value.push(data.clientCard);
      if (data.clientCardBack)
          $scope.imgNew[1].value.push(data.clientCardBack);
      // 上传营业执照
      $scope.imgNew[2].value = data.entrust;
      // 上传税务登记证与组织机构代码证 CodeCertificate
      $scope.imgNew[3].value = data.rental;
    }
 }
  function getPersonalInfo(){
    rest.post({
      methodId: '4021',
      data: {
        id: $scope.checkId
      },
      success: function(data) {
        if (data.code == "0") {
          init(data.res.data);
        } else {
          dialog.tips(data.res.msg);
        }
      },
      error: function() {}
    });
  }
});
